package com.example.helper;

import android.accessibilityservice.AccessibilityService;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityWindowInfo;
import android.widget.Toast;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class onAccessibilityEvent extends AccessibilityService {
    int search_count=20;
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        String  nowPackageName = event.getPackageName().toString();
        String  nowClassName = event.getClassName().toString();
        utils Helper=new utils();
        if(nowPackageName.equals("com.example.helper")&&nowClassName.equals("android.widget.Button")){
        int eventType = event.getEventType();
        switch (eventType) {
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                //界面点击
                AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
                String word=nodeInfo.findAccessibilityNodeInfosByViewId("com.liuxu.autorun:id/word").get(0).getText().toString();
                while (true) {
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
                    try {
                        Thread.sleep(1000);
                        Helper.Click(findNodesByText("天天快报"));
//                        Thread.sleep(15000);
//                    Toast.makeText(getApplicationContext(), "跳过", Toast.LENGTH_SHORT).show();
                        Helper.Back(findNodesById("com.tencent.reading:id/permission_guide_close"));
//                        Thread.sleep(2000);
//                    Toast.makeText(getApplicationContext(), "打开搜索", Toast.LENGTH_SHORT).show();
                        Helper.Click(findNodesById("com.tencent.reading:id/search_btn"));
//                        Thread.sleep(3000);
//                    Toast.makeText(getApplicationContext(), "填入文字", Toast.LENGTH_SHORT).show();
                        Helper.set_text(findNodesById("com.tencent.reading:id/inputSearch").get(0), word);
//                        Thread.sleep(2000);
                        Helper.Click(findNodesById("com.tencent.reading:id/btnCancel"));
//                        Thread.sleep(4000);
//                    Toast.makeText(getApplicationContext(), "点击进入", Toast.LENGTH_SHORT).show();
                        Thread.sleep(3000);
                        Helper.Click(findNodesById("com.tencent.reading:id/list_title_text"));
                        int time = 25;
                        search_count=1;
                        while (time > 0) {
                            if (findNodesById("com.tencent.reading:id/like_icon").size() == 0) {
                                Helper.swipe(300, 750, 300, 250, 300);
                                Thread.sleep(1000);
                                time -= 1;
                            } else {
                                Helper.Click(findNodesById("com.tencent.reading:id/like_icon"));
                                break;
                            }
                        }
                        search_count=20;
                        Helper.Click(findNodesById("com.tencent.reading:id/media_focus_view"));
                        Thread.sleep(2000);
                        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                        performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
                        Helper.execShellCmd("am force-stop com.tencent.reading");
                        Thread.sleep(1000);
                        Helper.execShellCmd("pm clear com.tencent.reading");
                    } catch (InterruptedException e) {
                        continue;
                    }
                }
//            case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
                //界面文字改动
//                break;
//            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
        }
        }
    }

    public List<AccessibilityNodeInfo> findNodesById(String viewId) throws InterruptedException {
        List<AccessibilityNodeInfo> result = new ArrayList<>();
        int time=search_count;
        while (result.size() == 0&&time>0){
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                result=nodeInfo.findAccessibilityNodeInfosByViewId(viewId);
            }
            Thread.sleep(1000);
            time-=1;
        }
        return result;
    }

    public List<AccessibilityNodeInfo> findNodesByText(String text) throws InterruptedException {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (nodeInfo != null) {
                return nodeInfo.findAccessibilityNodeInfosByText(text);
            }
        return Collections.emptyList();
    }
        public void onInterrupt () {
        }

    }